<?php
class fwDb
{
	/**
	 * SQL Query engine
	 *
	 * @var fwDbEngin
	 */
	private static $engin = null;

	private static function initEngin()
	{
		global $conf;
		if( self::$engin === null)
		{
			self::$engin = new fwDbEngin($conf['bdd']['host'], $conf['bdd']['login'], $conf['bdd']['password'], $conf['bdd']['database']);
		}
	}
	
	
	/**
	 * Gets the list of fields
	 *
	 * @param string $table_name name of the table
	 * @return array|false
	 */
	public static function getFields($table_name)
	{
		self::initEngin();
		return self::$engin->getFields($table_name);
	}

	/**
	 * Gets the list of tables
	 *
	 * @return array
	 */
	public static function getTables()
	{
		self::initEngin();
		return self::$engin->getTables();
	}

	/**
	 * Gets the list of indexes
	 *
	 * @param string $table_name nom de la table
	 * @return array|false
	 */
	public static function getIndexes($table_name)
	{
		self::initEngin();
		return self::$engin->getIndexes($table_name);
	}

	/**
	 * To retreive the contents of a query
	 *
	 * @param string $sql_query query to execute
	 * @return array|false
	 */
	public public static function getData($sql_query, $result_type=MYSQL_ASSOC)
	{
		self::initEngin();
		return self::$engin->getData($sql_query, $result_type=MYSQL_ASSOC);
	}

	/**
	 * To retreive the first row of a query
	 *
	 * @param string $sql_query query to execute
	 * @return array|false
	 */
	public static function getRow($sql_query, $result_type=MYSQL_ASSOC)
	{
		self::initEngin();
		return self::$engin->getRow($sql_query, $result_type=MYSQL_ASSOC);
	}

	/**
	 * To retreive the first cell of the first row of a query
	 *
	 * @param string $sql_query query to execute
	 * @return array array who conains all the fields of the one row
	 */
	public static function getCell($sql_query)
	{
		self::initEngin();
		return self::$engin->getCell($sql_query);
	}

	/**
	 * Sends an SQL query to MySQL (INSERT, UPDATE, DELETE...)
	 *
	 * @param string $sql_query query to execute
	 * @return int
	 */
	public static function setData($sql_query)
	{
		self::initEngin();
		return self::$engin->setData($sql_query);
	}

	/**
	 * Gets  the ID generated from the previous INSERT operation
	 *
	 * @return int
	 */
	public static function insertedId()
	{
		self::initEngin();
		return self::$engin->insertedId();
	}
}
?>